<?php
include_once ("../Login/databaseconnection.php");
$Role=$_POST['Role'];
$New_Role=$_POST['New_Role'];
if($Role==""&&$New_Role=="")
{
$Role="Teacher";
}
else if($New_Role!=""&&$Role!="")
{
$_SESSION['error']="Please select or type the role!";
}
else
{
if($New_Role!="")
{
$Role=$New_Role;
}
 $T_NIC_Number=$_POST['T_NIC_Number'];
  $First_Name=$_POST['First_Name'];
$Last_Name=$_POST['Last_Name'];
 $Date_Of_Birth=$_POST['Date_Of_Birth'];
$Gender=$_POST['Gender'];
 $Nature_Of_Appointment=$_POST['Nature_Of_Appointment'];
 $Service=$_POST['Service'];
 $SchName=$_POST['SchName'];
 $Specialised_In=$_POST['Specialised_In'];
 $New_Specialised_In=$_POST['New_Specialised_In'];
 $reg_Specia_In="";
 for($i=0;$i<sizeof($Specialised_In);$i++)
  {
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In=$Specialised_In[$i];
  }
  else
  {
  $reg_Specia_In=$reg_Specia_In.",".$Specialised_In[$i];
  }
  }
  if($New_Specialised_In!="")
  {
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In=$New_Specialised_In;
  }
  else
  {
  $reg_Specia_In=$reg_Specia_In.",".$New_Specialised_In;
  }
  }
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In="";
  }
	$name=explode(",",$SchName);
    $nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
if($Service=="")
{
$serv="0year";
}
if($Service!="")
{
$chars = str_split( $Service );
  $i=0;
while($i<strlen($Service))
{
$c=$chars[$i]; 
if($c=="y"||$c=="Y")
{
$st=$i;
break;
}
else
{
$i++;
}
}
if($st>0)
{
$ser=substr($Service,0,$st);
$ser_chars=str_split($ser);
for($i=0;$i<count($ser_chars);$i++)
{
if($ser_chars[$i]!="0"&&$ser_chars[$i]!="1"&&$ser_chars[$i]!="2"&&$ser_chars[$i]!="3"&&$ser_chars[$i]!="4"&&$ser_chars[$i]!="5"&&$ser_chars[$i]!="6"&&$ser_chars[$i]!="7"&&$ser_chars[$i]!="8"&&$ser_chars[$i]!="9")
{
$ser="0";
}
}
}
if($st==0)
{
$ser="0";
}
if($st=="")
{
$ser_chars=str_split($Service);
for($i=0;$i<count($ser_chars);$i++)
{
if($ser_chars[$i]!="0"&&$ser_chars[$i]!="1"&&$ser_chars[$i]!="2"&&$ser_chars[$i]!="3"&&$ser_chars[$i]!="4"&&$ser_chars[$i]!="5"&&$ser_chars[$i]!="6"&&$ser_chars[$i]!="7"&&$ser_chars[$i]!="8"&&$ser_chars[$i]!="9")
{
$ser="0";
}
else
{
$ser=$Service;
}
}
}
if((int) $ser>1)
{
$serv=$ser."years";
}
if((int) $ser<=1)
{
$serv=$ser."year";
}
}
$Professional_Qualifications=$_POST['Professional_Qualifications'];
$Achievements=$_POST['Achievements_Awards'];
$Achieve= mysql_real_escape_string($Achievements);
$Address=$_POST['Address'];
$Mobile_Tp_Number=$_POST['Mobile_Tp_Number'];
$Home_Tp_Number=$_POST['Home_Tp_Number'];
$Office_Tp_Number=$_POST['Office_Tp_Number'];
$Appointment_Date=$_POST['Appointment_Date'];
$Appointed_Subject=$_POST['Appointed_Subject'];
$Res_Team=$_POST['Res_Team'];
if($Res_Team=="")
{
$Res_Team="No";
}
$Training_Name=$_POST['Training_Name'];
$tra_id=mysql_query("SELECT * FROM training WHERE Training_Name='".$Training_Name."'");
if($r_id=mysql_fetch_array($tra_id))
{
$Training_Id=$r_id['Training_Id'];
}
$maxid=mysql_query("SELECT max(Member_Id) as maxId FROM resource_team");
while($rowid=mysql_fetch_array($maxid))
{
$m_Id=$rowid['maxId'];
 $inum=substr("$m_Id",1);
$inn=($inum+1);
$Member_Id='M';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$Member_Id=$Member_Id.'0';
}
$Member_Id=$Member_Id.$inn;
}
else
{
$Member_Id=$Member_Id.$inn;
}
}
$From_Date=$_POST['From_Date'];
$dehol="";
$Academic_Qualifications=$_POST['Academic_Qualifications'];
date_default_timezone_set("Europe/Paris");
    $t_d=date("Y-m-d");
$Profe_Qualifications= mysql_real_escape_string($Professional_Qualifications);
if($T_NIC_Number!=""&&$SchName!=""&&$First_Name!=""&&$Last_Name!=""&&$Date_Of_Birth!=""&&$Gender!=""&&$Nature_Of_Appointment!=""&&$Academic_Qualifications!=""&&$Service!=""&&$Address!=""&&$Appointment_Date!=""&&$Appointed_Subject!="")
{
$A_Qua="";
if($Res_Team=="No")
{
for($i=0;$i<sizeof($Academic_Qualifications);$i++)
  {
   if($Academic_Qualifications[$i]=="Degree_holder")
  {
 $dehol="yes";
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree=="")
  {
  $_SESSION['errordeg']="Please type your subject you got degree on it!";
  }
  else
  {
  if(empty($A_Qua))
  {
  $A_Qua="deg_".$degree;
  }
  else
  {
  $A_Qua=$A_Qua.",deg_".$degree;
  }
  }
  }
  else
  {
  if(empty($A_Qua))
  {
  $A_Qua=$Academic_Qualifications[$i];
  }
  else
  {
$A_Qua=$A_Qua.','.$Academic_Qualifications[$i];
}
}
}
if($dehol=="yes")
  {
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree!="")
  {
$sql=mysql_query("INSERT INTO teacher(Specialised_In,Role,T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Academic_Qualifications,Professional_Qualifications,Achievements_Awards,Service,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Appointment_Date,Appointed_Subject,Last_Updated_Date)VALUES('$reg_Specia_In','$Role','$T_NIC_Number','$School_Id','$First_Name','$Last_Name','$Date_Of_Birth','$Gender','$Nature_Of_Appointment','$A_Qua','$Profe_Qualifications','$Achieve','$serv','$Address','$Mobile_Tp_Number','$Home_Tp_Number','$Office_Tp_Number','$Appointment_Date','$Appointed_Subject','$t_d')");
if($sql)
  {
  $_SESSION['OK']="OK";
  $sqlrt=mysql_query("INSERT INTO resource_team(Training_Id,Member_Id,T_NIC_Number,From_Date,Last_Updated_Date)VALUES('$Training_Id','$Member_Id','$T_NIC_Number','$From_Date','$t_d')");
if($sqlrt)
  {
$_SESSION['errorrt']="Sorry unable to register this record into resource team!";
  }
if(!$sqlrt)
  {
$_SESSION['errorrt']="Sorry unable to register this record into resource team!";
  }
  $_SESSION['error']="This record is successfully registered!";
  
 }
if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
  }
  else
  {
  $sql=mysql_query("INSERT INTO teacher(Specialised_In,Role,T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Academic_Qualifications,Professional_Qualifications,Achievements_Awards,Service,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Appointment_Date,Appointed_Subject,Last_Updated_Date)VALUES('$reg_Specia_In','$Role','$T_NIC_Number','$School_Id','$First_Name','$Last_Name','$Date_Of_Birth','$Gender','$Nature_Of_Appointment','$A_Qua','$Profe_Qualifications','$Achieve','$serv','$Address','$Mobile_Tp_Number','$Home_Tp_Number','$Office_Tp_Number','$Appointment_Date','$Appointed_Subject','$t_d')");

if($sql)
  {
  $_SESSION['OK']="OK";
    $_SESSION['error']="This record is successfully registered!";
 }
if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }

  }
}
if($Res_Team=="Yes")
{
if($Training_Id==""||$Member_Id==""||$From_Date=="")
{
$_SESSION['errorrt']="Sorry unable to register this record into resource team!";
}
else
{
for($i=0;$i<sizeof($Academic_Qualifications);$i++)
  {
   if($i==0)
  {
  if($Academic_Qualifications[$i]=="Degree_holder")
  {
  $dehol="yes";
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree=="")
  {
  $A_Qua="deg_";
  $_SESSION['errordeg']="Please type your subject that you got degree on it!";
  }
  else
  {
  $A_Qua="deg_".$degree;
  }
  }
  else
  {
  $A_Qua=$Academic_Qualifications[$i];
  }
  }
  else
  {
  if($Academic_Qualifications[$i]=="Degree_holder")
  {
  $dehol="yes";
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree=="")
  {
  $A_Qua=$A_Qua.",deg_";
  $_SESSION['errordeg']="Please type your subject that you got degree on it!";
  }
  else
  {
  $A_Qua=$A_Qua.",deg_".$degree;
  }
  }
  else
  {
$A_Qua=$A_Qua.','.$Academic_Qualifications[$i];
}
 }
}
if($dehol=="yes")
  {
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree!="")
  {
$sql=mysql_query("INSERT INTO teacher(Specialised_In,Role,T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Academic_Qualifications,Professional_Qualifications,Achievements_Awards,Service,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Appointment_Date,Appointed_Subject,Last_Updated_Date)VALUES('$reg_Specia_In','$Role','$T_NIC_Number','$School_Id','$First_Name','$Last_Name','$Date_Of_Birth','$Gender','$Nature_Of_Appointment','$A_Qua','$Profe_Qualifications','$Achieve','$serv','$Address','$Mobile_Tp_Number','$Home_Tp_Number','$Office_Tp_Number','$Appointment_Date','$Appointed_Subject','$t_d')");
if($sql)
  {
  $_SESSION['OK']="OK";
   $_SESSION['error']="This record is successfully registered!";
  $sqlrt=mysql_query("INSERT INTO resource_team(Training_Id,Member_Id,T_NIC_Number,From_Date,Last_Updated_Date)VALUES('$Training_Id','$Member_Id','$T_NIC_Number','$From_Date','$t_d')");
if($sqlrt)
  {
 $_SESSION['error']="This record is successfully registered!";
  }
if(!$sqlrt)
  {
 $_SESSION['error']= $_SESSION['error']." Sorry unable to register this record into resource team!";
  }
 }
if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
  }
  else
  {
  $sql=mysql_query("INSERT INTO teacher(Specialised_In,Role,T_NIC_Number,School_Id,First_Name,Last_Name,Date_Of_Birth,Gender,Nature_Of_Appointment,Academic_Qualifications,Professional_Qualifications,Achievements_Awards,Service,Address,Mobile_Tp_Number,Home_Tp_Number,Office_Tp_Number,Appointment_Date,Appointed_Subject,Last_Updated_Date)VALUES('$reg_Specia_In','$Role','$T_NIC_Number','$School_Id','$First_Name','$Last_Name','$Date_Of_Birth','$Gender','$Nature_Of_Appointment','$A_Qua','$Profe_Qualifications','$Achieve','$serv','$Address','$Mobile_Tp_Number','$Home_Tp_Number','$Office_Tp_Number','$Appointment_Date','$Appointed_Subject','$t_d')");
if($sql)
  {
  $_SESSION['OK']="OK";
  $_SESSION['error']="This record is successfully registered!";
  $sqlrt=mysql_query("INSERT INTO resource_team(Training_Id,Member_Id,T_NIC_Number,From_Date,Last_Updated_Date)VALUES('$Training_Id','$Member_Id','$T_NIC_Number','$From_Date','$t_d')");
  if($sqlrt)
  {
 $_SESSION['error']="This record is successfully registered!";
  }
if(!$sqlrt)
  {
 $_SESSION['error']= $_SESSION['error']." Sorry unable to register this record into resource team!";
  }
 }
if(!$sql)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }

  }
}
  }
  }
  if($T_NIC_Number==""||$SchName==""||$First_Name==""||$Last_Name==""||$Date_Of_Birth==""||$Gender==""||$Nature_Of_Appointment==""||$Academic_Qualifications==""||$Service==""||$Address==""||$Appointment_Date==""||$Appointed_Subject=="")
	{
		$_SESSION['errorin']="Indexed details are required,Please fill all!";
	}
}
?>

